package com.lp.mgt.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lp.mgt.commons.CustomResult;
import com.lp.mgt.entities.Department;
import com.lp.mgt.services.DepartmentService;

@RestController
@RequestMapping("/dept")
public class DepartmentCtr {
	@Autowired
	private DepartmentService deptService;
	
	@RequestMapping("/getAllDepts")//用户管理部门树
	public CustomResult getDepartmentsToLoadTree(Department dept) {
		List<Department> nodes=deptService.getDepartmentsToLoadTree(dept);
		return new CustomResult().success(true, nodes);
	}
	
	@RequestMapping("/getDept2Tree")//岗位管理使用
	public CustomResult getDept2Tree(Department dept) {
		List<Department> nodes=deptService.getDept2Tree(dept);
		//返回一个节点
		Department d=new Department();
		d.setId("0");
		d.setText("MGT部门层级");
		d.setNodes(nodes);
		List<Department> rt=new ArrayList<Department>();
		rt.add(d);
		return new CustomResult().success(true,rt);
	}
	
	@PostMapping("/addDept")
	public CustomResult insertNewDepartment(Department dept) {
		String id=deptService.insertNewDepartment(dept);
		return new CustomResult().success(true, (Object)id);
	}
	
	@PostMapping("/updateDept")
	public CustomResult updateDepartmentWhenEdit(Department dept) {
		Boolean flag=deptService.updateDepartmentWhenEdit(dept);
		return new CustomResult().success(flag,"修改成功");
	}
	
	@GetMapping("/deleteDept")
	public CustomResult deleteDepartment(Department dept) {
		Boolean flag=deptService.deleteDepartmentById(dept);
		return new CustomResult().success(flag,"删除成功");
	}
	
	@PostMapping("/dragAnddrop")
	public CustomResult departmentDragAndDrop(Department dept) {
		Boolean flag=deptService.departmentDragAndDrop(dept);
		return new CustomResult().success(flag,"修改成功");
	}
}
